using UnityEngine;
using System.Collections;

namespace MenuItems
{
	/// <summary>
	/// Main menu.
	/// </summary>
	public class MainMenu : MonoBehaviour
	{
		////Unity inspector Variables
		public GUISkin skinForGUI;
		public float targetMenu = 0.0f;
		public float targetOptions = 92.25f;
		public float targetNewGame = -92.0f;
		public float cameraSpeed = 90.0f;

		//// Button Click Sound
		public AudioSource mySound;

		//Class variables
		private bool menuEnabled;
		private bool isMoving;
		private float buttonPosition;
		private float target;

		private NewGame ngame;
		private Options options;

		// Use this for initialization
		void Start ()
		{
			//Add a link to the event on the options menu
			//Point it to the setOptionsMenu() function
			ngame = NewGame.Instance;
			options = Options.Instance;
			ngame.NewGameStateChange += menuChangeListener;
			options.OptionsStateChange += menuChangeListener;

			target = 0.0f;
			menuEnabled = true;
			isMoving = false;
			buttonPosition = Screen.height - Screen.height * 0.222f;
		}

		void OnDestroy ()
		{
			//Remove listener from the event
			ngame.NewGameStateChange -= menuChangeListener;
			options.OptionsStateChange -= menuChangeListener;
		}
		
		void Update ()
		{
			if (isMoving) {
				float angle = Mathf.MoveTowardsAngle (transform.eulerAngles.y, target, cameraSpeed * Time.deltaTime);
				transform.eulerAngles = new Vector3 (0, angle, 0);

				if (target == targetOptions && inRange (targetOptions, angle)) {
					options.Visible = true;
					isMoving = false;
				} else if (target == targetNewGame && inRange (targetNewGame, angle)) {
					ngame.Visible = true;
					isMoving = false;
				} else if (target == targetMenu && inRange (targetMenu, angle)) {
					isMoving = false;
				}
			}
		}
		
		/// <summary>
		/// Listener for when options menu or new game menu enables and then disables main menu.
		/// </summary>
		/// <param name="state">If set to <c>true</c> state.</param>
		public void menuChangeListener (bool state)
		{
			menuEnabled = !state;
			if (menuEnabled) {
				isMoving = true;
				target = targetMenu;
			}
		}

		/// <summary>
		/// Camera rotating to check if it is within one of the main menu options.
		/// </summary>
		/// <returns><c>true</c>, if range was ined, <c>false</c> otherwise.</returns>
		/// <param name="targetAngle">Target angle.</param>
		/// <param name="currentAngle">Current angle.</param>
		private bool inRange (float targetAngle, float currentAngle)
		{
			if (targetAngle < 0) {
				targetAngle = 360 + targetAngle;
			}
			return((currentAngle < targetAngle + 1) && (currentAngle > targetAngle - 1));		
		}

		void OnGUI ()
		{
			if (menuEnabled & !isMoving) {
				GUI.skin = skinForGUI;
				if (GUI.Button (new Rect (Screen.width / 2 - 149, buttonPosition, 100, 32), "NewGame")) {
					// This code is executed when the Button is clicked
					isMoving = true;
					target = targetNewGame;
					mySound.Play ();
				}
				if (GUI.Button (new Rect (Screen.width / 2 - 50, buttonPosition, 100, 32), "Options")) {
					// This code is executed when the Button is clicked
					isMoving = true;
					target = targetOptions;
					mySound.Play ();
				}
				if (GUI.Button (new Rect (Screen.width / 2 + 49, buttonPosition, 100, 32), "Quit")) {
					// This code is executed when the Button is clicked
					Application.Quit ();
					mySound.Play ();
				}	
			}
		}
	}
}
